package com.canbot.u05.activity.guide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.bean.Audio;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zmp on 2019/4/10 17:08
 *
 * @author zmp
 * <p>
 * 导览语音选择界面
 */
public class GuideChooseAudioActivity extends BaseActivityNoTitle implements View.OnClickListener {


        private Dialog dialogLoading;

        private ToastTools toastTools;

        private String audioName;

        private String audioPath;

        private List<Audio> voicesData = Collections.synchronizedList(new ArrayList<Audio>());

        private MyAdapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_guide_choose_audio);
                toastTools = ToastTools.newInstance(this);
                initView();

        }

        private void initView() {
                Intent intent = getIntent();
                int type = intent.getIntExtra("type", AudioEvent.AUDIO_TYPE_AUDIO);
                if (type == AudioEvent.AUDIO_TYPE_AUDIO) {
                        audioName = intent.getStringExtra("content");
                        audioPath = intent.getStringExtra("path");
                }
                findViewById(R.id.guide_voice_save).setOnClickListener(this);
                findViewById(R.id.guide_voice_back).setOnClickListener(this);
                ListView listView = (ListView) findViewById(R.id.voiceListView);
                adapter = new MyAdapter();
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Audio audio = voicesData.get(position);
                                audioPath = audio.getPath();
                                audioName = audio.getName();
                                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, audioPath);
                                adapter.notifyDataSetChanged();
                        }
                });
                getData();
        }

        private void getData() {
                showLoadingTip();
                String url = "http://" + IpMsgConst.HEAD_SOCKET_HOST + ":7755/getAudioList";
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("path", "/uusound");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("t", result);
                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.HEAD_SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        List<Audio> list = JSON.parseArray(strMsgBean.getResult().toString(), Audio.class);
                                                        Collections.sort(list, new Comparator<Audio>() {
                                                                @Override
                                                                public int compare(Audio audio, Audio t1) {
                                                                        return audio.getName().compareTo(t1.getName());
                                                                }
                                                        });
                                                        voicesData.clear();
                                                        voicesData.addAll(list);
                                                        adapter.notifyDataSetChanged();
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                toastTools.showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });

        }

        private void showLoadingTip() {
                if (dialogLoading == null) {
                        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                }
                dialogLoading.show();
        }


        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.guide_voice_save:
                                setVoiceResult();
                                break;
                        case R.id.guide_voice_back:
                                setResult(RESULT_CANCELED);
                                finish();
                                break;
                        default:
                }
        }

        private void setVoiceResult() {
                setResult(RESULT_OK, new Intent()
                        .putExtra("type", AudioEvent.AUDIO_TYPE_AUDIO)
                        .putExtra("content", audioName)
                        .putExtra("path", audioPath));
                finish();
        }


        @Override
        protected void onPause() {
                super.onPause();
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stopSound");
        }


        private class MyAdapter extends BaseAdapter {


                private final LayoutInflater layoutInflater;

                MyAdapter() {
                        layoutInflater = LayoutInflater.from(GuideChooseAudioActivity.this);
                }

                @Override
                public int getCount() {
                        return voicesData == null ? 0 : voicesData.size();
                }

                @Override
                public Audio getItem(int position) {
                        return voicesData.get(position);
                }

                @Override
                public long getItemId(int position) {
                        return 0;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        if (convertView == null) {
                                convertView = layoutInflater.inflate(R.layout.guide_voice_lv_item, parent, false);
                                convertView.setTag(new MyHolder(convertView));
                        }
                        ((MyHolder) convertView.getTag()).update(voicesData.get(position));

                        return convertView;
                }
        }

        private class MyHolder {

                private final ImageView imageView;

                private final TextView textView;

                MyHolder(View convertView) {
                        imageView = (ImageView) convertView.findViewById(R.id.checkImageView);
                        textView = ((TextView) convertView.findViewById(R.id.contentTextView));
                }

                void update(Audio audio) {
                        imageView.setSelected(audio.getPath().equals(audioPath));
                        textView.setText(audio.getName());
                }
        }
}
